<?php


namespace app\api\library\order\cancelRefund;


use app\api\model\MallCollage;
use app\api\model\MallOrder;
use think\facade\Db;

class V
{
    public static function run($param)
    {
        $userId  = _checkEmptyParam($param[ 'userId' ], '用户ID不能为空');
        $orderId = _checkEmptyParam($param[ 'orderId' ], '订单ID不能为空');
        $order   = Db::name('mall_order')->where([['id', '=', $orderId], ['refund_status', 'in', [1, 10]], ['order_status', '=', 70]])->find();
        if (empty($order)) {
            _outPut(204, [], '订单不存在');
        }
        if ($order[ 'receipt_time' ] > 0) {
            $orderStatus = 60;
        } elseif ($order[ 'delivery_time' ] > 0) {
            $orderStatus = 50;
        } else {
            $collage = Db::name('mall_collage')->where('collage_id', $order[ 'collage_id' ])->find();
            if ($collage[ 'finish_time' ] > 0) {
                $orderStatus = 30;
            } else {
                $orderStatus = 10;
            }
        }
        if (false === MallOrder::where('id', $orderId)->update(['refund_status' => 0, 'order_status' => $orderStatus, 'submit_refund_time' => ''])) {
            _outPut(204, [], '订单退款失败');
        }
        $collageId = MallOrder::where('id', $orderId)->value('collage_id');
        if (false === MallCollage::where('collage_id', $collageId)->update(['status' => 1])) {
            _outPut(204, [], '订单退款失败');
        }
        _outPut(200, [], '申请退款成功');
    }
}